Skip to content

feat(cloudflare): Instrument sync KV#21316

Open
JPeer264 wants to merge 1 commit into
developfrom
jp/cloudflare-sync-kv-instrument
Open

feat(cloudflare): Instrument sync KV#21316
JPeer264 wants to merge 1 commit into
developfrom
jp/cloudflare-sync-kv-instrument

Conversation

@JPeer264
Copy link
Copy Markdown
Member

@JPeer264 JPeer264 commented Jun 3, 2026

closes #20830
closes JS-2443

This adds instrumentation for ctx.storage.kv.* (it only adds a span like with the async KV).

Cloudflare itself is having cloudflare-durable-object-sql as db.system.name, that is why we have it here now too.

@JPeer264 JPeer264 self-assigned this Jun 3, 2026
@JPeer264 JPeer264 requested a review from a team as a code owner June 3, 2026 08:32
@JPeer264 JPeer264 requested review from andreiborza and mydea and removed request for a team June 3, 2026 08:32
@linear-code
Copy link
Copy Markdown

linear-code Bot commented Jun 3, 2026

JS-2443

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 2decb59. Configure here.

@JPeer264 JPeer264 force-pushed the jp/cloudflare-sync-kv-instrument branch from 2decb59 to 091b09b Compare June 3, 2026 08:40
@JPeer264 JPeer264 requested a review from a team as a code owner June 3, 2026 08:40
@JPeer264 JPeer264 requested review from nicohrubec and removed request for a team June 3, 2026 08:41
@JPeer264 JPeer264 force-pushed the jp/cloudflare-sync-kv-instrument branch from 0526d91 to 26e3446 Compare June 3, 2026 13:52
Comment on lines +39 to +41
if (prop === 'kv' && original != null && typeof original === 'object') {
return instrumentDurableObjectSyncKvStorage(original as SyncKvStorage);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Accessing storage.kv repeatedly creates a new proxy object each time instead of reusing a cached one, causing unnecessary performance overhead.
Severity: MEDIUM

Suggested Fix

Use a WeakMap to cache the instrumented proxy for the kv property. On the first access, create the proxy, store it in the map, and return it. On subsequent accesses, return the cached proxy from the WeakMap. This aligns with the existing caching pattern in instrumentEnv.ts and avoids creating multiple proxy objects.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location:
packages/cloudflare/src/instrumentations/instrumentDurableObjectStorage.ts#L39-L41

Potential issue: The instrumentation for Durable Object storage creates a new proxy for
the `kv` property on every access. In scenarios where code makes multiple sequential
calls like `storage.kv.get(...)` and then `storage.kv.put(...)`, a new proxy object is
unnecessarily created for each call. This leads to increased object allocations and
garbage collection pressure, which can degrade performance in serverless environments.
This behavior is inconsistent with the caching pattern (using `WeakMap`) established in
other parts of the codebase, such as in `instrumentEnv.ts`.

Did we get this right? 👍 / 👎 to inform future reviews.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 3, 2026

size-limit report 📦

Path Size % Change Change
@sentry/browser 27.2 kB - -
@sentry/browser - with treeshaking flags 25.65 kB - -
@sentry/browser (incl. Tracing) 45.25 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 47.48 kB - -
@sentry/browser (incl. Tracing, Profiling) 50.24 kB - -
@sentry/browser (incl. Tracing, Replay) 84.87 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 74.44 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 89.58 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 102.2 kB - -
@sentry/browser (incl. Feedback) 44.36 kB - -
@sentry/browser (incl. sendFeedback) 32.02 kB - -
@sentry/browser (incl. FeedbackAsync) 37.11 kB - -
@sentry/browser (incl. Metrics) 28.27 kB - -
@sentry/browser (incl. Logs) 28.51 kB - -
@sentry/browser (incl. Metrics & Logs) 29.22 kB - -
@sentry/react 29.03 kB - -
@sentry/react (incl. Tracing) 47.52 kB - -
@sentry/vue 32.22 kB - -
@sentry/vue (incl. Tracing) 47.17 kB - -
@sentry/svelte 27.23 kB - -
CDN Bundle 29.57 kB - -
CDN Bundle (incl. Tracing) 47.82 kB - -
CDN Bundle (incl. Logs, Metrics) 31.08 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 49.06 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 70.36 kB - -
CDN Bundle (incl. Tracing, Replay) 85.22 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 86.39 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 91.08 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 92.23 kB - -
CDN Bundle - uncompressed 87.7 kB - -
CDN Bundle (incl. Tracing) - uncompressed 144.13 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 92.19 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 147.88 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 217 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 262.99 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 266.74 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 276.69 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 280.42 kB - -
@sentry/nextjs (client) 50.05 kB - -
@sentry/sveltekit (client) 45.71 kB - -
@sentry/core/server 75.94 kB - -
@sentry/core/browser 63.09 kB - -
@sentry/node-core 61.69 kB -0.01% -1 B 🔽
@sentry/node 130.4 kB +0.01% +1 B 🔺
@sentry/node - without tracing 74.06 kB +0.01% +2 B 🔺
@sentry/aws-serverless 86.26 kB - -
@sentry/cloudflare (withSentry) - minified 174.09 kB +0.29% +498 B 🔺
@sentry/cloudflare (withSentry) 434.92 kB +0.31% +1.31 kB 🔺

View base workflow run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cloudflare instrument Sync KV API

2 participants